library(plyr)
library(igraph)
library(fmsb)
library(Hmisc)
library(MASS)
library(stargazer)
library(tidyverse)
library(multcomp)

library(gsynth)
data(gsynth)
library(panelView)

setwd("~/Dropbox/China Huawei Paper/Production Materials/Replication/")
master.data <- read.csv("dataFile_China_Huawei.csv"); head(master.data)


# Polyarchy < 0.33
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<0.33,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Filtering")
out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nInternet Shutdowns")
out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Monitoring")
out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nArrests for Online Content")
out4$est.avg


# Polyarchy < 0.5
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<0.50,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Filtering")
out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nInternet Shutdowns")
out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Monitoring")
out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nArrests for Online Content")
out4$est.avg


# Polyarchy < 0.6
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<0.6,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Filtering")
out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nInternet Shutdowns")
out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Monitoring")
out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nArrests for Online Content")
out4$est.avg


# Polity
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$polity2<=0,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Filtering")
out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nInternet Shutdowns")
out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Monitoring")
out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nArrests for Online Content")
out4$est.avg



